package com.example.textswitchtestdemo01.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.textswitchtestdemo01.R;
import com.example.textswitchtestdemo01.bean.NewsInfo;
import com.example.textswitchtestdemo01.util.InitToolbar;
import com.example.textswitchtestdemo01.util.RetrofitUtil;

import java.text.SimpleDateFormat;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EighthDetailActivity extends AppCompatActivity {
    public static final int DEFAULT_VALUE = -1;
    private TextView tv_title_eight_detail;
    private TextView tv_time_eight_detail;
    private TextView tv_content_eight_detail;
    private VideoView vv_content_eight_detail;
    private int id;
    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private MediaController controller;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_eighth_detail);
        initView();
        InitToolbar.initToolbar(this, "详情", true);
        Intent intent = getIntent();
        id = intent.getIntExtra("id", DEFAULT_VALUE);



        getData();

    }

    private void getData() {

        if (DEFAULT_VALUE == id) {
            Toast.makeText(this, "获取失败", Toast.LENGTH_SHORT).show();
        } else {
            RetrofitUtil.getNewsById(id, new Callback<NewsInfo>() {
                @Override
                public void onResponse(Call<NewsInfo> call, Response<NewsInfo> response) {
                    List<NewsInfo.DataBean> data = response.body().getData();

                    if (data != null && !data.isEmpty()) {
                        NewsInfo.DataBean dataBean = data.get(0);
                        tv_title_eight_detail.setText(dataBean.getInformationName());
                        tv_content_eight_detail.setText(dataBean.getWords());
                        long l = dataBean.getTime() * 1000L;
                        tv_time_eight_detail.setText(sdf.format(l));
                        String video = dataBean.getVideo();
                        if (video != null && !video.isEmpty() && !video.equals("null")) {
                            vv_content_eight_detail.setVideoURI(Uri.parse("http://" + RetrofitUtil.IP + video));
                            vv_content_eight_detail.setMediaController(controller);
                             vv_content_eight_detail.start();
                            vv_content_eight_detail.requestFocus();


                        } else {
                            vv_content_eight_detail.setVisibility(View.GONE);
                            Toast.makeText(EighthDetailActivity.this, "no video", Toast.LENGTH_SHORT).show();
                        }

                    }
                }

                @Override
                public void onFailure(Call<NewsInfo> call, Throwable t) {
                    Toast.makeText(EighthDetailActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        }

    }

    private void initView() {
        tv_title_eight_detail = (TextView) findViewById(R.id.tv_title_eight_detail);
        tv_time_eight_detail = (TextView) findViewById(R.id.tv_time_eight_detail);
        tv_content_eight_detail = (TextView) findViewById(R.id.tv_content_eight_detail);
        vv_content_eight_detail = (VideoView) findViewById(R.id.vv_content_eight_detail);

        controller = new MediaController(EighthDetailActivity.this);
        controller.setAnchorView(this.vv_content_eight_detail);
        controller.setMediaPlayer(this.vv_content_eight_detail);

    }
}
